package com.jc.util;

import java.io.Serializable;

public class R<T> implements Serializable {

	private static final long serialVersionUID = -6170799082389272832L;

	public enum RCODE {

		SYS_ERROR(500, "系统错误"),
		PERMISSION_ERROR(2001, "没有权限"),
		SIGN_ERROR(5001, "签名校验失败"),
		PARAMETERS_ERROR(5002, "接口参数不正确"),
		DATA_ERROR(5003, "数据提交失败"),
		FILE_TYPE_ERROR(5004, "上传文件类型不符合要求"),
		FILE_NULL_ERROR(5005, "上传文件不能为空"),
		REQUEST_LIMIT_OUT(8002, "超出请求频率限制");

		RCODE(int code, String message) {
			setCode(code);
			setMessage(message);
		}

		public int getCode() {
			return code;
		}

		private void setCode(int code) {
			this.code = code;
		}

		public String getMessage() {
			return message;
		}

		private void setMessage(String message) {
			this.message = message;
		}

		private int code;
		private String message;

	}

	public R<T> error(int code, String msg, T content) {
		this.setCode(code);
		this.setMessage(msg);
		this.setSuccess(false);
		this.setContent(content);
		return this;
	}

	public R<T> error(RCODE rcode, T content) {
		return error(rcode.getCode(), rcode.getMessage(), content);
	}

	public R<T> error(RCODE rcode) {
		return error(rcode, null);
	}

	public R<T> ok(T content) {
		return ok(200, "Success", content);
	}

	public R<T> ok() {
		return ok(200, "Success", null);
	}

	public R<T> ok(int code, String msg, T content) {
		this.setCode(code);
		this.setMessage(msg);
		this.setSuccess(true);
		this.setContent(content);
		return this;
	}

	/**
	 * 成功标记
	 */
	private Boolean success;

	private Integer code;

	private String message;

	private T content;

	public Boolean getSuccess() {
		return success;
	}

	public void setSuccess(Boolean success) {
		this.success = success;
	}

	public Integer getCode() {
		return code;
	}

	public void setCode(Integer code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public T getContent() {
		return content;
	}

	public void setContent(T content) {
		this.content = content;
	}

}
